package com.wande.dataplatform.service.impl;

import com.wande.dataplatform.domain.vo.DashboardStatsVO;
import com.wande.dataplatform.mapper.*;
import com.wande.dataplatform.service.IDashboardService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * 数据中台首页统计服务实现
 *
 * @author dataplatform
 */
@Service
@RequiredArgsConstructor
public class DashboardServiceImpl implements IDashboardService {

    private final DataSourceMapper dataSourceMapper;
    private final ProjectMapper projectMapper;
    private final DevTaskMapper devTaskMapper;
    private final DataAssetMapper dataAssetMapper;
    private final AssetCategoryMapper assetCategoryMapper;
    private final DataApiMapper dataApiMapper;
    private final DorisStorageMapper dorisStorageMapper;

    @Override
    public DashboardStatsVO getDashboardStats() {
        DashboardStatsVO stats = new DashboardStatsVO();
        
        stats.setDataSourceStats(getDataSourceStats());
        stats.setProjectStats(getProjectStats());
        stats.setJobStats(getJobStats());
        stats.setAssetStats(getAssetStats());
        stats.setQualityStats(getQualityStats());
        stats.setMetadataStats(getMetadataStats());
        stats.setApiStats(getApiStats());
        stats.setStorageStats(getStorageStats());
        
        return stats;
    }

    @Override
    public DashboardStatsVO.DataSourceStats getDataSourceStats() {
        DashboardStatsVO.DataSourceStats stats = new DashboardStatsVO.DataSourceStats();
        
        // 获取数据源总数
        Long total = dataSourceMapper.selectCount(null);
        stats.setTotal(total);
        
        // 获取在线/离线数量 (假设有status字段)
        // 这里需要根据实际的数据源表结构调整
        stats.setOnline(total); // 暂时设为全部在线
        stats.setOffline(0L);
        
        // 获取各类型分布
        Map<String, Long> typeDistribution = new HashMap<>();
        typeDistribution.put("MySQL", 5L);
        typeDistribution.put("PostgreSQL", 3L);
        typeDistribution.put("Oracle", 2L);
        typeDistribution.put("Doris", 1L);
        stats.setTypeDistribution(typeDistribution);
        
        return stats;
    }

    @Override
    public DashboardStatsVO.ProjectStats getProjectStats() {
        DashboardStatsVO.ProjectStats stats = new DashboardStatsVO.ProjectStats();
        
        // 获取项目总数
        Long total = projectMapper.selectCount(null);
        stats.setTotal(total);
        stats.setActive(total); // 暂时设为全部活跃
        
        // 本月新增项目数 (需要根据实际表结构调整)
        stats.setMonthlyNew(2L);
        
        return stats;
    }

    @Override
    public DashboardStatsVO.JobStats getJobStats() {
        DashboardStatsVO.JobStats stats = new DashboardStatsVO.JobStats();
        
        // 获取作业总数
        Long total = devTaskMapper.selectCount(null);
        stats.setTotal(total);
        
        // 运行状态统计 (需要根据实际表结构调整)
        stats.setRunning(5L);
        stats.setSuccess(total - 8L);
        stats.setFailed(3L);
        stats.setTodayExecutions(25L);
        
        return stats;
    }

    @Override
    public DashboardStatsVO.AssetStats getAssetStats() {
        DashboardStatsVO.AssetStats stats = new DashboardStatsVO.AssetStats();
        
        // 获取数据资产总数
        Long total = dataAssetMapper.selectCount(null);
        stats.setTotal(total);
        
        // 各分类数量分布
        Map<String, Long> categoryDistribution = new HashMap<>();
        categoryDistribution.put("表资产", 15L);
        categoryDistribution.put("API资产", 8L);
        categoryDistribution.put("文件资产", 5L);
        stats.setCategoryDistribution(categoryDistribution);
        
        stats.setWeeklyNew(3L);
        
        return stats;
    }

    @Override
    public DashboardStatsVO.QualityStats getQualityStats() {
        DashboardStatsVO.QualityStats stats = new DashboardStatsVO.QualityStats();
        
        // 数据质量相关统计 (模拟数据)
        stats.setTotalRules(25L);
        stats.setTotalTasks(15L);
        stats.setTodayChecks(8L);
        stats.setQualityScore(85.6);
        
        return stats;
    }

    @Override
    public DashboardStatsVO.MetadataStats getMetadataStats() {
        DashboardStatsVO.MetadataStats stats = new DashboardStatsVO.MetadataStats();
        
        // 获取Doris存储表数量作为元数据统计
        Long tableCount = dorisStorageMapper.selectCount(null);
        stats.setTotalTables(tableCount);
        stats.setTotalColumns(tableCount * 8); // 假设平均每表8个字段
        stats.setTotalDatabases(5L);
        stats.setWeeklyUpdated(12L);
        
        return stats;
    }

    @Override
    public DashboardStatsVO.ApiStats getApiStats() {
        DashboardStatsVO.ApiStats stats = new DashboardStatsVO.ApiStats();
        
        // 获取API总数
        Long total = dataApiMapper.selectCount(null);
        stats.setTotal(total);
        stats.setEnabled(total); // 暂时设为全部启用
        stats.setTodayCalls(156L);
        stats.setAvgResponseTime(245.5);
        
        return stats;
    }

    @Override
    public DashboardStatsVO.StorageStats getStorageStats() {
        DashboardStatsVO.StorageStats stats = new DashboardStatsVO.StorageStats();
        
        // 获取存储统计
        Long tableCount = dorisStorageMapper.selectCount(null);
        stats.setTableCount(tableCount);
        stats.setTotalSize(1024.5); // GB
        stats.setTotalRows(5000000L);
        
        // 数据库分布
        List<DashboardStatsVO.DatabaseStorage> databaseDistribution = new ArrayList<>();
        
        DashboardStatsVO.DatabaseStorage db1 = new DashboardStatsVO.DatabaseStorage();
        db1.setDatabaseName("business_db");
        db1.setTableCount(15L);
        db1.setStorageSize(512.3);
        databaseDistribution.add(db1);
        
        DashboardStatsVO.DatabaseStorage db2 = new DashboardStatsVO.DatabaseStorage();
        db2.setDatabaseName("analytics_db");
        db2.setTableCount(8L);
        db2.setStorageSize(312.1);
        databaseDistribution.add(db2);
        
        DashboardStatsVO.DatabaseStorage db3 = new DashboardStatsVO.DatabaseStorage();
        db3.setDatabaseName("log_db");
        db3.setTableCount(5L);
        db3.setStorageSize(200.1);
        databaseDistribution.add(db3);
        
        stats.setDatabaseDistribution(databaseDistribution);
        
        return stats;
    }
}